package com.aiwu.memory;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.aiwu.library.ui.pop.TextLinearListPop;
import com.aiwu.library.ui.widget.transformerstip.gravity.TipGravity;
import com.aiwu.library.util.DisplayUtil;
import com.aiwu.library.util.DrawableUtil;
import com.aiwu.library.util.StartActivityUtil;

import org.citra.citra_emu.R;

import java.util.ArrayList;
import java.util.List;

public class MemoryFirstFragment extends Fragment implements MemoryManager.KeyBoardOffsetCallback {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_memory_first, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view) {
        TextView tvSelectType = view.findViewById(R.id.tv_select_type);
        tvSelectType.setCompoundDrawables(null, null, DrawableUtil.getDrawableDimenRes(R.drawable.emu_lib_icon_arrow_down, R.dimen.qb_px_13, R.dimen.qb_px_9), null);
        tvSelectType.setOnClickListener(this::showSelectTypePop);
        EditText etNum = view.findViewById(R.id.et_num);
        etNum.requestFocus();
        MemoryManager.disableShowInput(etNum, true, this);
        CheckBox cbDepthSearch = view.findViewById(R.id.cb_depth_search);
        cbDepthSearch.setCompoundDrawables(DrawableUtil.getDrawableDimenRes(R.drawable.emu_lib_selector_cb_rec, R.dimen.qb_px_21), null, null, null);
        cbDepthSearch.setChecked(MemoryManager.getInstance().isDepthSearch());
        cbDepthSearch.setOnClickListener(v -> MemoryManager.getInstance().switchDepthSearch());
        view.findViewById(R.id.iv_tip).setOnClickListener(v -> StartActivityUtil.startBrowserIntent("https://support.qq.com/products/491138/faqs/139137"));
    }

    private void showSelectTypePop(View anchorView) {
        new TextLinearListPop<String>(anchorView) {
            @Override
            protected void initView(View contentView) {
                super.initView(contentView);
                listAdapter.setOnItemClickListener((adapter, view, position) -> {
                    dismissTip();
                    MemoryManager.getInstance().changeSearchType(position == 0 ? MemoryManager.SEARCH_TYPE_NORMAL : MemoryManager.SEARCH_TYPE_FUZZY);
                });
            }

            @Override
            protected List<String> getListData() {
                List<String> list = new ArrayList<>();
                list.add(getString(R.string.search_type_normal));
                list.add(getString(R.string.search_type_fuzzy));
                return list;
            }

            @Override
            protected void convertListItem(@NonNull ListViewHolder helper, String item) {
                helper.tvTitle.setText(item);
                helper.ivCurrent.setVisibility(View.GONE);
            }
        }.setBackgroundDimEnabled(false)
                .setTipGravity(TipGravity.TO_BOTTOM_CENTER)
                .setFixedWidth(anchorView.getWidth())
                .setTipOffsetYRes(R.dimen.qb_px_15)
                .show();
    }

    @Override
    public boolean setKeyBoardOffset(ViewGroup.MarginLayoutParams layoutParams) {
        if (DisplayUtil.isPortrait()) {
            layoutParams.setMargins(0, getView().getHeight() - getResources().getDimensionPixelOffset(R.dimen.qb_px_45), 0, 0);
            return true;
        }
        return false;
    }
}
